<?php

namespace Api\Controller;

use Think\Controller;
use Api\Library\CryptAes;

class CommonController extends Controller {

    //用于存储解密后的参数数组
    protected $param;
    //加密对象
    protected $crypt;

    protected function _initialize() {
        import('@.Library.CryptAes');
        $this->crypt = new CryptAes();
        //对参数的解密处理，成功后存入字段
        $param_string = I('request.para');
        if (empty($param_string)) {
            $this->returnData(2, 'para参数不能为空');
        }
        $param_array = json_decode($this->crypt->decode($param_string), true);
        if (empty($param_array)) {
            $this->returnData(2, '参数错误');
        }
        if (empty($param_array['action'])) {
            $this->returnData(2, 'action不能为空');
        }
        $this->param = $param_array;
        $method_name = $param_array['action'];
        $this->$method_name();
        die;
    }

    /**
     * 接口返回数据
     * @param int $status 非0值为异常
     * @param string $info 提示信息
     * @param array $data 返回数据
     */
    protected function returnData($status = 2, $info = '', $data = array()) {
        if (empty($data)) {
            $data = $this->param;
        }
        $return['status'] = $status;
        $return['info'] = $info;
        $return['data'] = $data;
        echo $this->crypt->encode(json_encode($return));
        die;
    }

    /**
     * 空方法
     */
    public function _empty() {
        $this->returnData(2, 'action不存在');
    }

}
